package com;

/**
 * 停止线程Thread.interrupt()
 *
 * @author bysun
 */
class TestInterrupt extends Thread {

    @Override
    public void run() {
        super.run();
        for (int i = 0; i < 500000; i++) {
            System.out.println("i:{" + i + "}");
        }
    }

}

/**
 * 利用this.isInterrupted()是否被打断的状态停止线程, 只能停止循环, 对循环外不起作用
 */
class Interrupt01 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 5000000; i++) {
            System.out.println("i:{" + i + 1 + "}");
            if (this.isInterrupted()) {
                System.out.println("停止线程!!!");
                break;
            }
        }
        System.out.println("for循环外的执行");
    }
}

/**
 * 利用抛出异常打断线程
 */
class Interrupt02 extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 5000000; i++) {
                System.out.println("i:{" + i + 1 + "}");
                if (this.isInterrupted()) {
                    System.out.println("停止线程");
                    throw new InterruptedException();
                }
            }
            System.out.println("for循环外的执行");
        } catch (InterruptedException e) {

        }
    }
}

/**
 * 沉睡中停止线程01, 先睡眠, 再打断
 * 在sleep状态下, 被打断, 会进入catch, 并且清除停止状态值, 变成false
 */
class Interrupt03 extends Thread {

    @Override
    public void run() {
        super.run();
        try {
            System.out.println("线程开始");
            Thread.sleep(50000);
            System.out.println("线程开始");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 沉睡中停止线程02 先进行打断, 后进行睡眠
 * 打断后, 当线程进入睡眠状态时, 会进入catch, 并且清除停止状态值, 变成false
 */
class Interrupt04 extends Thread {

    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i < 50000; i++) {
                System.out.println("i:{" + (i + 1) + "}");
            }
            System.out.println(this.isInterrupted());
            System.out.println("线程开始");
            Thread.sleep(50000);
            System.out.println("线程开始");
        } catch (InterruptedException e) {
            System.out.println("进入catch");
            System.out.println(this.isInterrupted());
            e.printStackTrace();
        }

    }
}

/**
 * 利用return停止线程
 */
class Interrupt05 extends Thread {

    @Override
    public void run() {
        int i = 1;
        super.run();
        while (i < 1000) {
            if (this.isInterrupted()) {
                System.out.println("停止线程");
                return;
            }
            System.out.println("i:{" + (i ++) + "}");
        }
        System.out.println("while下面");
    }
}

public class Demo07 {

    public static void main(String[] args) {
        try {
//            TestInterrupt testThread = new TestInterrupt();
//            testThread.start();
//            Thread.sleep(100L);
//            testThread.interrupt();
//            System.out.println("是否停止1:" + testThread.isInterrupted());
//            System.out.println("是否停止2:" + testThread.isInterrupted());
//            Thread.interrupted();
//            Interrupt01 thread = new Interrupt01();
//            Interrupt02 thread = new Interrupt02();
//            Interrupt03 thread = new Interrupt03();
            Interrupt05 thread = new Interrupt05();
            thread.start();
            Thread.sleep(100);
            thread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        Interrupt04 thread = new Interrupt04();
//        thread.start();
//        thread.interrupt();
//        System.out.println("end");
    }
}
